package cn.kgc.musicserver.controller;


import cn.hutool.core.util.ObjUtil;
import cn.kgc.musicserver.domain.dto.Result;
import cn.kgc.musicserver.domain.dto.ResultUtil;
import cn.kgc.musicserver.domain.entity.ListSong;
import cn.kgc.musicserver.service.IListSongService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 歌单包含歌曲列表 前端控制器
 * </p>
 *
 * @author 王玉林
 * @since 2025-06-21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/list-song")
public class ListSongController {
    private final IListSongService listSongService;
    // 增
    @Transactional
    @RequestMapping("add")
    public Result addListSong(ListSong listSong) {
        // 查询 song_id、song_list_id 是否存在
        QueryWrapper<ListSong> wrapper = new QueryWrapper<>();
        wrapper.eq("song_id", listSong.getSongId());
        wrapper.eq("song_list_id", listSong.getSongListId());
        ListSong listSong1 = listSongService.getOne(wrapper);
        if(ObjUtil.isNotNull(listSong1)){
            return ResultUtil.returnFail("添加失败，歌单已有该歌曲");
        }
        boolean res = listSongService.saveOrUpdate(listSong);
        if(!res){
            return ResultUtil.returnFail("音乐添加歌单失败");
        }
        return ResultUtil.returnSuccess("音乐添加歌单成功");
    }

}
